<template>
  <div class="app-container">
    <el-button type="primary" @click="handleAddNotice">创建通知</el-button>

    <el-table :data="noticeList" style="width: 100%;margin-top:30px;" border>
      <el-table-column align="center" label="id" width="80">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="通知主题" width="180">
        <template slot-scope="scope">
          {{ scope.row.subject }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="通知内容" width="100">
        <template slot-scope="scope">
          {{ scope.row.content }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="发布人" width="220">
        <template slot-scope="scope">
          {{ scope.row.publisher }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="发布时间" width="120">
        <template slot-scope="scope">
          {{ scope.row.time }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="handleEdit(scope)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDelete(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页查询 -->
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
      :page-sizes="pageSizes" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="dataTotal"
      v-show="dataTotal>0">
    </el-pagination>
    <!-- 弹框 -->
    <NoticeDialog :dialogType="dialogType" :dialogVisible.sync="dialogVisible" :notice="notice"
      @dialogDataAdd="addNoticeList" @dialogDataChange="changeNoticeList"></NoticeDialog>
  </div>
</template>

<script>
  import {
    MessageBox,
    Message
  } from 'element-ui'
  import {
    getNoticeByPage,
    deleteNotice
  } from '@/api/notice'
  import {
    deepClone
  } from '@/utils'
  import NoticeDialog from './components/notice_dialog.vue'

  export default {
    components: {
      NoticeDialog,
    },
    data() {
      return {
        noticeList: [],
        notice: {
        },
        dialogVisible: false,
        dialogType: 'new',
        // 分页
        currentPage: 1,
        pageSize: 10,
        pageSizes: [10, 15, 20, 100],
        dataTotal: 0
      }
    },
    computed: {},
    created() {
      this.getNotice()
    },
    methods: {
      // 获取数据库通知信息
      async getNotice() {
        const {
          data
        } = await getNoticeByPage(this.currentPage, this.pageSize)
        this.dataTotal = data.total
        this.noticeList = data.data
      },
      // 添加通知
      handleAddNotice() {
        this.dialogType = 'new'
        this.dialogVisible = true
        this.notice = {}
      },
      // 修改通知
      handleEdit(scope) {
        this.notice = deepClone(scope.row)
        this.dialogType = 'edit'
        this.dialogVisible = true
      },
      // 删除通知
      handleDelete({
        $index,
        row
      }) {
        this.$confirm('确定删除?', 'Warning', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
          .then(async () => {
            const res = await deleteNotice(row.id)
            if (res.code === 1) {
              this.noticeList.splice($index, 1)
              this.$message({
                type: 'success',
                message: res.msg
              })
            }
          })
          .catch(err => {
            console.error(err)
          })
      },
      // 接收子组件传入的 Notice
      addNoticeList(data) {
        this.noticeList.push(data)
      },
      changeNoticeList(id, data) {
        for (let index = 0; index < this.noticeList.length; index++) {
          if (this.noticeList[index].id === data.id) {
            this.noticeList.splice(index, 1, Object.assign({}, data))
            break
          }
        }
      },
      // 分页功能
      handleSizeChange(val) {
        this.pageSize = val
        this.getNotice()
      },
      handleCurrentChange(val) {
        this.currentPage = val
        this.getNotice()
      }
      //
    }
  }
</script>

<style lang="scss" scoped>
  .app-container {
    .roles-table {
      margin-top: 30px;
    }

    .permission-tree {
      margin-bottom: 30px;
    }
  }
</style>
